<?php
/**
 * 后台菜单管理
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Page;
use Common\Library\Category;
class MenuController extends CommonController{
	private $msg=array();
	public function main(){
		$this->redirect('index');
	}
	public function index(){
		if(!$this->checkPermission('Menu')) $this->jump();
		$Menu=D('Menu');
		$data=array();
		if(I('title')) $data['title']=I('title');
		if(I('status')) $data['status']=I('status');	
		$list=$Menu->getMenus($data);
		$list=Category::unlimitedForLevel($list,'',0,0,'menu_id','pid','title');
		$count=count($list);
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));		
		$show=$Page->show();
		$list=array_splice($list,$Page->firstRow,$Page->listRows);
		$url['p']=I('get.p','');
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();
	}
	public function oper($menu_id=0){
		if(!$menu_id && !$this->checkPermission('AddMenu')) $this->jump();
		$languages=get_langs();
		$menu=D('Menu')->getMenu($menu_id);
		if($menu_id && empty($menu)) $this->jump(L('error_404'));
		if(IS_POST){
			$menu=I('post.');
			if($this->validateForm()){	
				if(D('Menu')->viewMenu($menu_id,$menu)){
					session('success',$menu_id?L('success_edit'):L('success_add'));
					$url['p']=I('get.p','');
					$this->redirect('index',$url);
				}				
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');				
			}			
		}
		$data=array(
			'start'		=>	1,
			'limit'		=>	1000,
			'menu_id'	=>	$menu_id,
			'status'	=>	1,
			'is_show'	=>	1
			);
		$menus=D('Menu')->getMenus($data);

		$this->assign('menus',Category::unlimitedForLevel($menus,'',0,0,'menu_id','pid'));
		$this->assign('msg',$this->msg);
		$this->assign('menu',$menu);
		$this->assign('menu_id',$menu_id);
		$this->assign('languages',$languages);
		$this->display();
	}
	private function validateForm(){
		$mneu_id=I('menu_id',0,'trim');
		if($menu_id && !$this->checkPermission('EditMenu')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach ($data['menu_description'] as $language=>$value) {
			if(mb_strlen($value['title'])<2||mb_strlen($value['title'])>100){
				$this->msg['title'][$language]		=	L('error_title');
			}
		}
		if(strlen($data['url'])<3||strlen($data['url']>100)){
			$this->msg['url']=L('error_url');
		}else{
			$map=array(
				'url'=>$data['url'],
				'menu_id'=>array('neq',$menu_id)
				);
			$vili_url=M('Menu')->where($map)->find();
			if(!empty($vili_url)){
				$this->msg['url']=L('error_url_uiq');
			}
		}
		return !$this->msg;
	}
	public function del(){
		if($this->checkPermission('DeleteMenu')){
			$count=0;
			$MenuModel=D('Menu');
			foreach (I('selected') as $menu_id) {
				$count += $MenuModel->del($menu_id);
			}			
			session('success',L('success_delete',array('count'=>$count)));			
		}else{
			session('danger',L('error_not_permission'));
		}
		$this->redirect('index');
	}
}